﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinformBlazor.Components;

namespace WinFormBlazorDesigner.Ext
{
    public static class Ext
    {
        /// <summary>
        /// 根据控件类型,生成默认的控件
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ControlInfo? GetControl(this ContrloType? type)
        {
            if (type == null) return null;
            ControlInfo con = new ControlInfo();
            con.Type = type.Value;
            con.Style.position = EntityStore.Position.absolute;
            switch (type)
            {
                case ContrloType.Form:
                    break;
                case ContrloType.Textbox:
                    con.Class.SelfVerticalAlignment = EntityStore.SelfVerticalAlignment.align_self_start;
                    break;
                case ContrloType.Button:
                    break;
                case ContrloType.Panel:
                    con.Style.height = "100px";
                    con.Style.width = "250px";
                    break;
                case ContrloType.Label:
                    break;
                case ContrloType.CheckBox:
                    con.Class.SelfVerticalAlignment = EntityStore.SelfVerticalAlignment.align_self_center;
                    break;
            }
            return con;
        }
        /// <summary>
        /// 根据id找到这个控件的父级控件
        /// </summary>
        /// <param name="ctr"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static ControlInfo? FindParentById(this ControlInfo ctr, string id)
        {

            foreach (ControlInfo item in ctr.Childs)
            {
                // 检查当前控件的名称是否匹配
                if (item.Id == id)
                {
                    return ctr; // 找到匹配项，返回它
                }
                // 递归地在子控件中查找
                var found = item.FindParentById(id);
                if (found != null)
                {
                    return found; // 如果在子控件中找到了，返回结果
                }
            }
            return null; // 未找到匹配项，返回null
        }

    }
}
